import {
  Controller,
  Get,
  Post,
  Body,
  Param,
  Query,
  UseInterceptors,
  ValidationPipe,
} from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiBearerAuth } from '@nestjs/swagger';
import { TransformInterceptor } from '../../../common/interceptors/transform.interceptor';
import { SecurityLogService } from '../services/security-log.service';
import { CreateSecurityLogDto } from '../dto/create-security-log.dto';
import { QuerySecurityLogDto } from '../dto/query-security-log.dto';
import { UpdateSecurityLogDto } from '../dto/update-security-log.dto';
import { SecurityLogResponseDto } from '../dto/security-log-response.dto';
import { QuerySecurityStatisticsDto } from '../dto/query-security-statistics.dto';
import { PaginationResponseDto } from '../../../common/dto/pagination.dto';
import { CurrentUser } from '../../../common/decorators/current-user.decorator';
import { ApiName } from '../../../common/decorators/api-name.decorator';
import { ApiResponseDto } from '../../../common/dto/response.dto';

@ApiTags('系统日志')
@Controller('security-log')
@UseInterceptors(TransformInterceptor)
@ApiBearerAuth()
export class SecurityLogController {
  constructor(private readonly securityLogService: SecurityLogService) {}

  @Get("page")
  @ApiOperation({ summary: '分页获取安全日志列表' })
  @ApiName('分页获取安全日志列表')
  @ApiResponse({
    status: 200,
    description: '获取成功',
    type: PaginationResponseDto,
  })
  async findAll(@Query() queryDto: QuerySecurityLogDto) {
    return await this.securityLogService.findSecurityLogsWithPagination(queryDto);
  }

  @Get('statistics')
  @ApiOperation({ summary: '获取安全统计信息' })
  @ApiName('获取安全统计信息')
  @ApiResponse({
    status: 200,
    description: '获取成功',
  })
  async getStatistics(@Query() queryDto: QuerySecurityStatisticsDto) {
    const start = queryDto.startTime ? new Date(queryDto.startTime) : undefined;
    const end = queryDto.endTime ? new Date(queryDto.endTime) : undefined;
    return await this.securityLogService.getSecurityStatistics(start, end);
  }

  @Get(':id')
  @ApiOperation({ summary: '获取安全日志详情' })
  @ApiName('获取安全日志详情')
  @ApiResponse({
    status: 200,
    description: '获取成功',
    type: SecurityLogResponseDto,
  })
  async findOne(@Param('id') id: string) {
    return await this.securityLogService.findById(id);
  }

  // @Post('batch-mark-handled')
  // @ApiOperation({ summary: '批量标记为已处理' })
  // @ApiName('批量标记为已处理')
  // @ApiResponse({
  //   status: 200,
  //   description: '操作成功',
  // })
  // async batchMarkAsHandled(
  //   @Body() body: { ids: string[]; handledNote?: string },
  //   @CurrentUser() user: any,
  // ) {
  //   return await this.securityLogService.batchMarkAsHandled(
  //     body.ids,
  //     user.id,
  //     body.handledNote,
  //   );
  // }
}
